package redis

import (
	"context"
	"github.com/pkg/errors"
)

// Conn represents a connection to a Redis server.
type Conn interface {
	// Close closes the connection.
	Close() error

	// Err returns a non-nil value when the connection is not usable.
	Err() error

	// Do sends a command to the server and returns the received reply.
	Do(commandName string, args ...interface{}) (reply interface{}, err error)

	// Send writes the command to the client's output buffer.
	Send(commandName string, args ...interface{}) error

	// Flush flushes the output buffer to the Redis server.
	Flush() error

	// Receive receives a single reply from the Redis server
	Receive() (reply interface{}, err error)
}

func Get(ctx context.Context) (c Conn) {
	c, _ = GetConn(ctx)
	return
}

func GetConn(ctx context.Context) (c Conn, err error) {
	if redisPool == nil {
		return nil, errors.New("redis pool not setup")
	}
	return redisPool.GetContext(ctx)
}
